<?php
/**
 *
 * html_order_form.php
 *
 * (c) 2015 by PhongND
 *
 * @author PhongND
 * @package wpGForm
 * @version $Revision$
 *
 */

// WordPress Google Form Plugin 'Team' Custom Post Type
define('WPGFORM_CPT_FORM', 'wpgform') ;
?>


 <!-- render Order Head here! -->
<!-- <?php echo 'html_order_form.php'; ?> -->
<form method="post" id="frm_order" action="">

<!-- http://stackoverflow.com/questions/11895476/bootstrap-element-100-width -->
<div class="container-fluid div_order_form">
    <div class="row">
		<div class="col-md-12">
		<p style="font-size:30px;">Hóa đơn</p>
		</div>
		
    </div>
	
	<!-- row: head -->
    <div class="row">
        <div class="col-md-4">
			<li>
			<p style="color:red;margin-bottom:22px">Dấu sao (*) là những giá trị bắt buộc</p>
			</li>
			<li>
				<p class="element">Mã đơn hàng<span style="color:red"> *</span></p>  
				<label><input id="ma_don_hang"name="ma_don_hang"type="text"value="<?php
				if(isset($_POST["ma_don_hang"]))
				{
					echo $_POST["ma_don_hang"];
				}
				?>" required minlength="3" maxlength="15"/></label>
			</li>
			<li></li>
			
			<li>
				<p class="element"style="margin-top:22px">Ngày đặt hàng</p>
				<label><input id="ngay_dat_hang"name="ngay_dat_hang"type="text"value="<?php
				if(isset($_POST["ngay_dat_hang"]))
				{
					echo $_POST["ngay_dat_hang"];
				}
				?>"/></label>
			</li>
			<li>
				<p class="element"style="margin-top:22px">Loại hóa đơn</p>
				<select>
					<option value="B">B</option>
					<option value="N">N</option>
					<option value="C">C</option>
				</select>
			</li>
        </div>
        <div class="col-md-4">
            <li style="height: 12px;"><p></p></li>
			<p class="element"style="margin-top:22px">Giảm giá</p>
			<label><input id="giam_gia"name="giam_gia"type="text"value="<?php
			if(isset($_POST["giam_gia"]))
			{
				echo $_POST["giam_gia"];
			}
			?>"/></label>
			<p class="element"style="margin-top:22px">Mã khách hàng</p>
			<label><input id="ma_khach_hang"name="ma_khach_hang"type="text"value="<?php
			if(isset($_POST["ma_khach_hang"]))
			{
				echo $_POST["ma_khach_hang"];
			}
			?>"/></label>
			<p class="element"style="margin-top:22px">Lý do giảm</p>
			<select id="ly_do_giam"name="ly_do_giam">
				<option value="KHAITRUONG">Khai Trương</option>
				<option value="G_MEMBER">G_MEMBER</option>
				<option value="S_MEMBER">S_MEMBER</option>
				<option value="B_MEMBER">B_MEMBER</option>
			</select>

        </div>
    </div>
	
	<!-- row: details -->
    <div class="row">
		<!-- //render Order Item  here!  -->
		<br/>
		<div class="col-md-12">
			
		<table class="tblOrderDetail">
			<thead>
				<tr>
					<th>Mã hàng</th>
					<th>Tên hàng</th>
					<th>Số lượng</th>
					<th>Đơn giá</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><input type="text" name="ma_hang[]" class="ma_hang"> </input></td>
					<td><input type="text" name="ten_hang[]" class="ten_hang"> </input></td>
					<td><input type="text" name="so_luong[]" class="so_luong"> </input></td>
					<td><input type="text" name="don_gia[]" class="don_gia"> </input></td>
				</tr>
			</tbody>
			<tfoot>
				
				<tr>
					<td colspan="0">
					<a id="add_item" href="#" onclick="return add_more_items(this)" class="btn btn-success">Thêm dòng</a>
					</td>
				</tr>
			</tfoot>
		</table>

		</div>
    </div>
	
	<!-- row: foot (submit, note...) -->
    <div class="row">
		<div class="col-md-12">
			<p style="margin-top:22px"><label><input id="btn_submit"type="submit"value="GHI NHẬN ĐƠN HÀNG"/></label><p>
		</div>
		
    </div>
	
</div>

</form>



<?php
	// var_dump($_POST);
	// var_dump($arr_options);
	// var_dump(self::$options);
$arr_options = self::$options; //nonoAAA!!! dùng 1 post_type=wpgform chac ko sao...
$arrOrder = $_POST;

if(isset($arrOrder["ma_don_hang"]))
{
	include_once('Google_Spreadsheet.php');
	$u = $arr_options['sendto'];// 'dongthuyphong@gmail.com'; // 
	$p = $arr_options['password']; //'123456asdASD'; // 
	$spread01 = new Google_Spreadsheet($u, $p); //  
	$spread01->useSpreadsheet($arr_options['spreadsheet_name']); //'spread01' // 'order_database'
	$spread01->useWorksheet($arr_options['worksheet_name'] ); //'CT_Row' // 'order_data'
		
	foreach ($arrOrder['ma_hang'] as $key => $val)
	{
		if(isset($val) & trim($val) != '')
		{
			$row = array
			(
				'Ma Don Hang' => $arrOrder['ma_don_hang']
				,'Ma Khach Hang' => $arrOrder['ma_khach_hang']
				,'Giam Gia' => $arrOrder['giam_gia']
				,'Ly Do Giam' => $arrOrder['ly_do_giam']
				,'Ma hang' => $val
				, 'Ten hang' => $arrOrder['ten_hang'][$key]
				, 'So luong' => $arrOrder['so_luong'][$key]
				, 'Don gia' => $arrOrder['don_gia'][$key]
			 // , 'Thanh tien' => '=C3*D3'
			);
			
			$arrUpdate = array('Thanh tien' => '=J%s*K%s'); 
			// $arrUpdate = array('Thanh tien' => '=C3*D3'); 
			if ($spread01->addRow($row, $arrUpdate))
			
				echo 'Form data successfully stored using Google Spreadsheet';
			else 
				echo 'Error, unable to store spreadsheet data';
		}
		else
		{
			continue;
		}
		
	
	}
	
	
	// $ma_don_hang = $_POST["ma_don_hang"];
	// echo "<table><tr><td>";
	
	// echo "$ma_don_hang";
	// echo "</td></tr></table>";
}
?>



